//
//  KXMLNode.m
//  Ghac 4S
//
//  Created by apple on 11-9-6.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "KXMLNode.h"
#import "KXPath.h"
@interface KXMLNode(private)
-(NSArray*) searchNode:(KXMLNode*)sourceNode forPath:(KXPath*)path range:(NSUInteger)range;
@end



@implementation KXMLNode
@synthesize __name__ = m_name;
@synthesize __type__ = m_type;
@synthesize __attributes__ = m_attribute;
@synthesize __childrens__ = m_childrens;
@synthesize __parent__ = m_parent;
@synthesize __nodeValue__ = m_nodeValue;
-(id)init
{
    if ((self = [super init]))
    {
        m_type = 0;
        self.__name__ = Nil;
        self.__attributes__ = [[NSMutableDictionary alloc] init];
        self.__childrens__ = [[NSMutableArray alloc] init];
        self.__parent__ = Nil;
    }
    return self;
}
-(NSString *)description
{
    NSString* str;
    NSMutableString* attributesStr = [[NSMutableString alloc] init];
    if (self.__attributes__ != Nil)
    {
        NSEnumerator* enu = [self.__attributes__ keyEnumerator];
        NSString* key;
        while (key = [enu nextObject])
        {
            [attributesStr appendFormat:@" %@=\"%@\"", key, [self.__attributes__ objectForKey:key]];
        }
    }
    if (self.__childrens__.count > 0)
    {
        NSMutableString* childrensStr = [[NSMutableString alloc] init];
        for (KXMLNode* child in self.__childrens__)
        {
            [childrensStr appendFormat:@"%@\n", child.description];
        }
        if (self.__nodeValue__ != Nil)
        {
            [childrensStr appendString:self.__nodeValue__];
        }
        str = [NSString stringWithFormat:@"<%@%@>\n%@\n<%@/>", self.__name__, attributesStr, childrensStr, self.__name__];
        [childrensStr release];
    }
    else
    {
        if (self.__nodeValue__ == Nil)
        {
            str = [NSString stringWithFormat:@"<%@%@/>", self.__name__, attributesStr];
        }
        else
        {
            str = [NSString stringWithFormat:@"<%@%@>%@<%@/>", self.__name__, attributesStr, self.__nodeValue__, self.__name__];
        }
        
    }
    [attributesStr release];
    return [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}
-(NSArray *)arrayByXPath:(KXPath *)path
{
    NSMutableArray* result = [NSMutableArray array];
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    [result addObjectsFromArray:[self searchNode:self forPath:path range:0]];
    [pool drain];
    return result;
//    return [self searchNode:self forPath:path range:0];
}
-(NSArray*) searchNode:(KXMLNode*)sourceNode forPath:(KXPath*)path range:(NSUInteger)range
{
    NSMutableArray* mutableArray = [[[NSMutableArray alloc] init] autorelease];
    NSArray* result;
    NSArray* childs = [sourceNode.__childrens__ filteredArrayUsingPredicate:[path predicatePathAtIndex:range]];
    
    if (range < path.length - 1) 
    {
//        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        for (KXMLNode* node in childs)
        {
            NSArray* array = [self searchNode:node forPath:path range:range + 1];
            [mutableArray addObjectsFromArray:array];
        }
//        [pool drain];
        result = mutableArray;
    }
    else
    {
        
        if (path.predicate == Nil)
        {
            result = childs;
        }
        else
        {
            result = [childs filteredArrayUsingPredicate:path.predicate];
        }
         
    }
    return result;
}
//-(id)valueForKey:(NSString *)key
//{
//    if (key isEqualToString:@"
//}
-(id)valueForUndefinedKey:(NSString *)key
{
//    NSLog(@"key in %@", key);
    if ([key characterAtIndex:0] == '@')
    {
        return [self.__attributes__ valueForKeyPath:[key stringByReplacingOccurrencesOfString:@"@" withString:@""]];
    }
    NSArray* array = [self.__childrens__ filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"__name__ == %@", key]];
    if (array.count > 0)
    {
        return [array objectAtIndex:0];
    }
    else
    {
        return Nil;
    }

}
-(void)dealloc
{
    NSLog(@"node %@ removed", self.__name__);
    self.__name__ = Nil;
    m_type = 0;
    self.__attributes__ = Nil;
    self.__childrens__ = Nil;
    self.__parent__ = Nil;
    self.__nodeValue__ = Nil;
    [super dealloc];
}

@end

